!-------------------------------------------------------------LICENSE--------------------------------------------------------------!
!                                                                                                                                  !
!The MAP code is written in Fortran language for magnetohydrodynamics (MHD) calculation with the adaptive mesh refinement (AMR)    !
!and Message Passing Interface (MPI) parallelization.                                                                              !
!                                                                                                                                  !
!Copyright (C) 2012                                                                                                                !
!Ronglin Jiang                                                                                                                     !
!rljiang@ssc.net.cn                                                                                                                !
!585 Guoshoujing Road. Pudong, Shanghai, P.R.C. 201203                                                                             !
!                                                                                                                                  !
!This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License         !
!as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.             !
!                                                                                                                                  !
!This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of    !
!MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.                        !
!                                                                                                                                  !
!You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software     !
!Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.                                                   !
!                                                                                                                                  !
!-------------------------------------------------------------LICENSE--------------------------------------------------------------!

!==============================================================================================================================================|
subroutine amr (base)
!==============================================================================================================================================|

   use parameters
   use block_define
   use interfaces

   implicit none

   type(block), pointer, intent(inout) :: base

   integer(4) :: i, i_p, i_block

   type(block), pointer :: child

!----------------------------------------------------------------------------------------------------------------------------------------------|
   if (base%t .gt. t_regrid) then
      max_level = 1
      len_regrid = 1d30
      do i_block = 1, ini_blocks
         child => base%child(i_block)
         call regrid (child)
      enddo

      t_regrid = base%t + 2 * ng * min_dt / cfl
   endif

   min_dt = base_dt * 2.0d0 ** (1 - max_level)

!   call block_info (base)

   do i_block = 1, ini_blocks
      child => base%child(i_block)
      call integrate (child)
   enddo

   do i_block = 1, ini_blocks
      child => base%child(i_block)
      call block_boundary (child)
   enddo

   do i_block = 1, ini_blocks
      child => base%child(i_block)
      do i = ng + 1, ng + nx_no_bnd
         i_p = i + (i_block - 1) * nx_no_bnd
         base%ro(i_p) = child%ro(i)
         base%mx(i_p) = child%mx(i)
         base%my(i_p) = child%my(i)
         base%mz(i_p) = child%mz(i)
         base%bx(i_p) = child%bx(i)
         base%by(i_p) = child%by(i)
         base%bz(i_p) = child%bz(i)
         base%en(i_p) = child%en(i)
      enddo
   enddo
   base%t = base%t + min_dt

!print*, 123

!   do i_block = 1, ini_blocks
!!      child => base%child(5)%child(2)%child(2)
!      child => base%child(i_block)
!      do i = 5, 12
!         if (isnan(child%ro(i))) then
!            print*, child%ro
!            print*, child%id, child%lv, n_blocks
!            print*, child%child(2)%child(2)%ro, child%child(2)%child(2)%nc, child%child(2)%child(2)%lv
!            print*, base_dt, min_dt
!            pause
!         endif
!      enddo
!   enddo

!if( n_blocks .eq. 16) call block_info (base)
!print*, min_dt, max_level
!print*, base%child(2)%child(2)%child(2)%ro
!print*, base%child(3)%child(1)%child(1)%ro
!print*, base%child(1)%ro
!print*, base%child(2)%ro
!print*, base%child(3)%ro
!print*, base%child(4)%ro
!pause

   return
end subroutine amr
